package section6_5;

import java.util.Date;
import java.util.concurrent.DelayQueue;

/**
 * Created by Hyman on 2017/6/15.
 */
public class Task implements Runnable {

    private int id;
    private DelayQueue<Event> queue;

    public Task(int id, DelayQueue<Event> queue) {
        this.id = id;
        this.queue = queue;
    }

    /**
     * 核心方法，产生1000事件，每个事件有相同的激活时间，将这些事件存储在延迟队列中
     */
    @Override
    public void run() {
        Date now = new Date();
        Date delay = new Date();
        delay.setTime(now.getTime() + id * 1000); //用实际日期加上当前task编号id对应的秒数
        System.out.printf("Thread %s: %s\n", id, delay);

        for (int i = 0; i < 100; i++) {
            Event event = new Event(delay);
            queue.add(event);
        }
    }
}
